Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / feature / node / src / androidMain / kotlin / org / meshtastic / feature / node / compass / AndroidPhoneLocationProvider.kt

Displaying Raw • Download

feature/node/src/androidMain/kotlin/org/meshtastic/feature/node/compass/AndroidPhoneLocationProvider.kt 97ec245a70ef792e54a9e21a2d79c4832ced1d25 (97ec245a) Text, 7.50 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.feature.node.compass

Tff7b72import T7ee787android.Manifest
Tff7b72import T7ee787android.annotation.SuppressLint
Tff7b72import T7ee787android.content.Context
Tff7b72import T7ee787android.location.Location
Tff7b72import T7ee787android.location.LocationManager
Tff7b72import T7ee787android.os.Looper
Tff7b72import T7ee787androidx.core.content.ContextCompat
Tff7b72import T7ee787androidx.core.location.LocationListenerCompat
Tff7b72import T7ee787androidx.core.location.LocationManagerCompat
Tff7b72import T7ee787androidx.core.location.LocationRequestCompat
Tff7b72import T7ee787kotlinx.coroutines.channels.awaitClose
Tff7b72import T7ee787kotlinx.coroutines.flow.Flow
Tff7b72import T7ee787kotlinx.coroutines.flow.callbackFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.flowOn
Tff7b72import T7ee787org.koin.core.annotation.Single
Tff7b72import T7ee787org.meshtastic.core.di.CoroutineDispatchers

Tf0883e@Single
Tff7b72class T56d364AndroidPhoneLocationProviderTb4b4b4(Tff7b72private Tff7b72val Te6edf3contextTb4b4b4: Te6edf3ContextTb4b4b4, Tff7b72private Tff7b72val Te6edf3dispatchersTb4b4b4: Te6edf3CoroutineDispatchersTb4b4b4) Tb4b4b4:
Te6edf3PhoneLocationProvider Tb4b4b4{

Tf0883e@SuppressLintTb4b4b4(Ta5d6ff"Ta5d6ffMissingPermissionTa5d6ff"Tb4b4b4)
Tff7b72override Tff7b72fun Td2a8fflocationUpdatesTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3FlowTff7b72<Te6edf3PhoneLocationStateTff7b72> Tff7b72= Te6edf3callbackFlow Tb4b4b4{
Tff7b72val Te6edf3locationManager Tff7b72= Te6edf3contextTb4b4b4.Te6edf3getSystemServiceTb4b4b4(Te6edf3ContextTb4b4b4.Te6edf3LOCATION_SERVICETb4b4b4) Tff7b72as? Te6edf3LocationManager
Tff7b72if Tb4b4b4(Te6edf3locationManager Tff7b72=Tff7b72= Tff7b72nullTb4b4b4) Tb4b4b4{
Te6edf3trySendTb4b4b4(Te6edf3PhoneLocationStateTb4b4b4(Te6edf3permissionGranted Tff7b72= Tff7b72falseTb4b4b4, Te6edf3providerEnabled Tff7b72= Tff7b72falseTb4b4b4)Tb4b4b4)
Te6edf3closeTb4b4b4(Tb4b4b4)
Tff7b72returnTf0883e@callbackFlow
Tb4b4b4}

Tff7b72if Tb4b4b4(Tff7b72!Te6edf3hasLocationPermissionTb4b4b4(Tb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3trySendTb4b4b4(Te6edf3PhoneLocationStateTb4b4b4(Te6edf3permissionGranted Tff7b72= Tff7b72falseTb4b4b4, Te6edf3providerEnabled Tff7b72= Tff7b72falseTb4b4b4)Tb4b4b4)
Te6edf3closeTb4b4b4(Tb4b4b4)
Tff7b72returnTf0883e@callbackFlow
Tb4b4b4}

Tff7b72var Te6edf3lastLocationTb4b4b4: Te6edf3Location? Tff7b72= Tff7b72null

Tff7b72fun Td2a8ffsendUpdateTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3trySendTb4b4b4(
Te6edf3PhoneLocationStateTb4b4b4(
Te6edf3permissionGranted Tff7b72= Tff7b72trueTb4b4b4,
Te6edf3providerEnabled Tff7b72= Te6edf3LocationManagerCompatTb4b4b4.Te6edf3isLocationEnabledTb4b4b4(Te6edf3locationManagerTb4b4b4)Tb4b4b4,
Te6edf3location Tff7b72= Te6edf3lastLocationTff7b72?.Te6edf3toPhoneLocationTb4b4b4(Tb4b4b4)Tb4b4b4,
Tb4b4b4)Tb4b4b4,
Tb4b4b4)
Tb4b4b4}

Tff7b72val Te6edf3listener Tff7b72=
Tff7b72object Tb4b4b4: T56d364LocationListenerCompat Tb4b4b4{
Tff7b72override Tff7b72fun Td2a8ffonLocationChangedTb4b4b4(Te6edf3locationTb4b4b4: Te6edf3LocationTb4b4b4) Tb4b4b4{
T8b949e// Subscribing to both GPS and NETWORK providers means coarse Wi-Fi/cell fixes
T8b949e// would otherwise overwrite a fresh, accurate GPS fix on every callback,
T8b949e// making the compass distance/bearing jitter between two positions
T8b949e// (see issue #4864). Apply the canonical isBetterLocation filter so we
T8b949e// prefer the most accurate, recent fix and fall back to network only
T8b949e// when no usable GPS fix is available.
Tff7b72if Tb4b4b4(Te6edf3isBetterLocationTb4b4b4(Te6edf3locationTb4b4b4, Te6edf3lastLocationTb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3lastLocation Tff7b72= Te6edf3location
Te6edf3sendUpdateTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}

Tff7b72override Tff7b72fun Td2a8ffonProviderEnabledTb4b4b4(Te6edf3providerTb4b4b4: Tffa657StringTb4b4b4) Tff7b72= Te6edf3sendUpdateTb4b4b4(Tb4b4b4)

Tff7b72override Tff7b72fun Td2a8ffonProviderDisabledTb4b4b4(Te6edf3providerTb4b4b4: Tffa657StringTb4b4b4) Tff7b72= Te6edf3sendUpdateTb4b4b4(Tb4b4b4)
Tb4b4b4}

Tff7b72val Te6edf3locationRequest Tff7b72=
Te6edf3LocationRequestCompatTb4b4b4.Te6edf3BuilderTb4b4b4(Te6edf3MIN_UPDATE_INTERVAL_MSTb4b4b4)
Tb4b4b4.Te6edf3setMinUpdateDistanceMetersTb4b4b4(T79c0ff0fTb4b4b4)
Tb4b4b4.Te6edf3setQualityTb4b4b4(Te6edf3LocationRequestCompatTb4b4b4.Te6edf3QUALITY_HIGH_ACCURACYTb4b4b4)
Tb4b4b4.Te6edf3buildTb4b4b4(Tb4b4b4)

Tff7b72val Te6edf3providers Tff7b72= Te6edf3listOfTb4b4b4(Te6edf3LocationManagerTb4b4b4.Te6edf3GPS_PROVIDERTb4b4b4, Te6edf3LocationManagerTb4b4b4.Te6edf3NETWORK_PROVIDERTb4b4b4)

Tff7b72try Tb4b4b4{
Te6edf3lastLocation Tff7b72=
Te6edf3providers
Tb4b4b4.Te6edf3mapNotNull Tb4b4b4{ Te6edf3provider Tff7b72-Tff7b72> Te6edf3locationManagerTb4b4b4.Te6edf3getLastKnownLocationTb4b4b4(Te6edf3providerTb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3reduceOrNull Tb4b4b4{ Te6edf3bestTb4b4b4, Te6edf3candidate Tff7b72-Tff7b72>
Tff7b72if Tb4b4b4(Te6edf3isBetterLocationTb4b4b4(Te6edf3candidateTb4b4b4, Te6edf3bestTb4b4b4)Tb4b4b4) Te6edf3candidate Tff7b72else Te6edf3best
Tb4b4b4}

Te6edf3sendUpdateTb4b4b4(Tb4b4b4)

Te6edf3providersTb4b4b4.Te6edf3forEach Tb4b4b4{ Te6edf3provider Tff7b72-Tff7b72>
Tff7b72if Tb4b4b4(Te6edf3provider Tff7b72in Te6edf3locationManagerTb4b4b4.Te6edf3allProvidersTb4b4b4) Tb4b4b4{
Te6edf3LocationManagerCompatTb4b4b4.Te6edf3requestLocationUpdatesTb4b4b4(
Te6edf3locationManagerTb4b4b4,
Te6edf3providerTb4b4b4,
Te6edf3locationRequestTb4b4b4,
Te6edf3listenerTb4b4b4,
Te6edf3LooperTb4b4b4.Te6edf3getMainLooperTb4b4b4(Tb4b4b4)Tb4b4b4,
Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4} Tff7b72catch Tb4b4b4(Te6edf3securityExceptionTb4b4b4: Te6edf3SecurityExceptionTb4b4b4) Tb4b4b4{
Te6edf3trySendTb4b4b4(Te6edf3PhoneLocationStateTb4b4b4(Te6edf3permissionGranted Tff7b72= Tff7b72falseTb4b4b4, Te6edf3providerEnabled Tff7b72= Tff7b72falseTb4b4b4)Tb4b4b4)
Te6edf3closeTb4b4b4(Te6edf3securityExceptionTb4b4b4)
Tff7b72returnTf0883e@callbackFlow
Tb4b4b4}

Te6edf3awaitClose Tb4b4b4{ Te6edf3LocationManagerCompatTb4b4b4.Te6edf3removeUpdatesTb4b4b4(Te6edf3locationManagerTb4b4b4, Te6edf3listenerTb4b4b4) Tb4b4b4}
Tb4b4b4}
Tb4b4b4.Te6edf3flowOnTb4b4b4(Te6edf3dispatchersTb4b4b4.Te6edf3ioTb4b4b4)

Tff7b72private Tff7b72fun Td2a8ffhasLocationPermissionTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657Boolean Tff7b72=
Te6edf3ContextCompatTb4b4b4.Te6edf3checkSelfPermissionTb4b4b4(Te6edf3contextTb4b4b4, Te6edf3ManifestTb4b4b4.Te6edf3permissionTb4b4b4.Te6edf3ACCESS_FINE_LOCATIONTb4b4b4) Tff7b72=Tff7b72=
Te6edf3androidTb4b4b4.Te6edf3contentTb4b4b4.Te6edf3pmTb4b4b4.Te6edf3PackageManagerTb4b4b4.Te6edf3PERMISSION_GRANTED Tff7b72|Tff7b72|
Te6edf3ContextCompatTb4b4b4.Te6edf3checkSelfPermissionTb4b4b4(Te6edf3contextTb4b4b4, Te6edf3ManifestTb4b4b4.Te6edf3permissionTb4b4b4.Te6edf3ACCESS_COARSE_LOCATIONTb4b4b4) Tff7b72=Tff7b72=
Te6edf3androidTb4b4b4.Te6edf3contentTb4b4b4.Te6edf3pmTb4b4b4.Te6edf3PackageManagerTb4b4b4.Te6edf3PERMISSION_GRANTED

Tff7b72private Tff7b72fun Te6edf3LocationTb4b4b4.Td2a8fftoPhoneLocationTb4b4b4(Tb4b4b4) Tff7b72=
Te6edf3PhoneLocationTb4b4b4(Te6edf3latitude Tff7b72= Te6edf3latitudeTb4b4b4, Te6edf3longitude Tff7b72= Te6edf3longitudeTb4b4b4, Te6edf3altitude Tff7b72= Te6edf3altitudeTb4b4b4, Te6edf3timeMillis Tff7b72= Te6edf3timeTb4b4b4)

Tff7b72companion Tff7b72object Tb4b4b4{
Tff7b72private Tff7b72const Tff7b72val Te6edf3MIN_UPDATE_INTERVAL_MS Tff7b72= T79c0ff1Te6edf3_000L
Tff7b72private Tff7b72const Tff7b72val Te6edf3SIGNIFICANTLY_NEWER_MS Tff7b72= T79c0ff2 Tff7b72* T79c0ff6T79c0ff0 Tff7b72* T79c0ff1T79c0ff0T79c0ff0T79c0ff0L
Tff7b72private Tff7b72const Tff7b72val Te6edf3SIGNIFICANTLY_LESS_ACCURATE_M Tff7b72= T79c0ff2T79c0ff0T79c0ff0f

T8b949e/**
* Canonical Android "is this fix better than the last one?" comparison (adapted from the framework's
* LocationListener guide). Without this, subscribing to GPS_PROVIDER and NETWORK_PROVIDER simultaneously causes
* coarse Wi-Fi/cell fixes to overwrite recent fine GPS fixes, making the compass distance and bearing jump
* between two positions (issue #4864).
*/
Tf0883e@SuppressTb4b4b4(Ta5d6ff"Ta5d6ffReturnCountTa5d6ff"Tb4b4b4)
Tff7b72internal Tff7b72fun Td2a8ffisBetterLocationTb4b4b4(Te6edf3candidateTb4b4b4: Te6edf3LocationTb4b4b4, Te6edf3currentTb4b4b4: Te6edf3Location?Tb4b4b4)Tb4b4b4: Tffa657Boolean Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3current Tff7b72=Tff7b72= Tff7b72nullTb4b4b4) Tff7b72return Tff7b72true

Tff7b72val Te6edf3timeDelta Tff7b72= Te6edf3candidateTb4b4b4.Te6edf3time Tff7b72- Te6edf3currentTb4b4b4.Te6edf3time
Tff7b72val Te6edf3isSignificantlyNewer Tff7b72= Te6edf3timeDelta Tff7b72> Te6edf3SIGNIFICANTLY_NEWER_MS
Tff7b72val Te6edf3isSignificantlyOlder Tff7b72= Te6edf3timeDelta Tff7b72< Tff7b72-Te6edf3SIGNIFICANTLY_NEWER_MS
Tff7b72val Te6edf3isNewer Tff7b72= Te6edf3timeDelta Tff7b72> T79c0ff0

T8b949e// A much newer fix is always preferred even if accuracy is worse — the device
T8b949e// has likely moved, so a stale "accurate" fix is worse than a fresh coarse one.
Tff7b72if Tb4b4b4(Te6edf3isSignificantlyNewerTb4b4b4) Tff7b72return Tff7b72true
Tff7b72if Tb4b4b4(Te6edf3isSignificantlyOlderTb4b4b4) Tff7b72return Tff7b72false

Tff7b72val Te6edf3accuracyDelta Tff7b72= Te6edf3candidateTb4b4b4.Te6edf3accuracy Tff7b72- Te6edf3currentTb4b4b4.Te6edf3accuracy
Tff7b72val Te6edf3isMoreAccurate Tff7b72= Te6edf3accuracyDelta Tff7b72< T79c0ff0f
Tff7b72val Te6edf3isLessAccurate Tff7b72= Te6edf3accuracyDelta Tff7b72> T79c0ff0f
Tff7b72val Te6edf3isSignificantlyLessAccurate Tff7b72= Te6edf3accuracyDelta Tff7b72> Te6edf3SIGNIFICANTLY_LESS_ACCURATE_M
Tff7b72val Te6edf3isFromSameProvider Tff7b72= Te6edf3candidateTb4b4b4.Te6edf3provider Tff7b72=Tff7b72= Te6edf3currentTb4b4b4.Te6edf3provider

Tff7b72return Tff7b72when Tb4b4b4{
Te6edf3isMoreAccurate Tff7b72-Tff7b72> Tff7b72true
Te6edf3isNewer Tff7b72&Tff7b72& Tff7b72!Te6edf3isLessAccurate Tff7b72-Tff7b72> Tff7b72true
Te6edf3isNewer Tff7b72&Tff7b72& Tff7b72!Te6edf3isSignificantlyLessAccurate Tff7b72&Tff7b72& Te6edf3isFromSameProvider Tff7b72-Tff7b72> Tff7b72true
Tff7b72else Tff7b72-Tff7b72> Tff7b72false
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.07s